import { getLoginData, getStudentNumber } from "@/api/admin";

export async function GetLoginData(): Promise<{ dates: string[], adminLoginCounts: number[], studentLoginCounts: number[], teacherLoginCounts: number[] }>
{
    const responseData = await getLoginData();
    const dates: string[] = [];
    const adminLoginCounts: number[] = [];
    const studentLoginCounts: number[] = [];
    const teacherLoginCounts: number[] = [];

    Object.keys(responseData.data).forEach(userType => {
        const userData = responseData.data[userType];
        const userDates: string[] = [];
        const userLoginCounts: number[] = [];

        userData.forEach((item: Record<string, number>) => {
            const date = Object.keys(item)[0];
            const loginCount = item[date];

            userDates.push(date);
            userLoginCounts.push(loginCount);
        });
        if (userType === 'admin') {
            adminLoginCounts.push(...userLoginCounts);
            dates.push(...userDates);
        } else if (userType === 'student') {
            studentLoginCounts.push(...userLoginCounts);
        } else if (userType === 'teacher') {
            teacherLoginCounts.push(...userLoginCounts);
        }
    });

    return { dates, adminLoginCounts, studentLoginCounts, teacherLoginCounts };
}

export async function GetStudentNumber(): Promise<{ value: number, name: string }[]>
{
    const responseData = await getStudentNumber();
    const studentData: { value: number, name: string }[] = [];

    responseData.data.forEach((item: { number: number, class: string }) => {
        const value = item.number;
        const name = item.class;

        studentData.push({ value, name });
    });

    return studentData;
}